package uc;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import cn.onekit.CALLBACK;
import co.chuangdian.oflauncher.R;
import java.util.ArrayList;
import java.util.List;

public class FlowerMenu extends RelativeLayout {

	private Context context;
	// private int leftMargin = 0, bottomMargin = 0;
	private final int buttonWidth = 58;// ͼƬ���
	private final int r = 100;// �뾶
	private final int maxTimeSpent = 200;// �������ʱ
	private final int minTimeSpent = 80;// ��̶�����ʱ
	private int intervalTimeSpent;// ÿ����2����ʱ����
	private List<FlowerMenuItem> btns = new ArrayList<FlowerMenuItem>();
	private ImageView btn_menu;
	private LayoutParams params;
	private float angle;// ÿ����ť֮��ļн�

	public FlowerMenu(Context context) {
		super(context);
		this.context = context;
	}

	public FlowerMenu(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		View view = LayoutInflater.from(context).inflate(R.layout.uc_flower_menu, this);
		btn_menu = (ImageView) view.findViewById(R.id.btn_menu);
	}

	public void showMenu(int x, int y) {
		btn_menu.setVisibility(View.VISIBLE);
		isLeft = (x < this.getMeasuredWidth() / 2);
		LayoutParams layoutParams0 = (LayoutParams) btn_menu.getLayoutParams();
		layoutParams0.leftMargin = x - dp2px(buttonWidth) / 2;
		layoutParams0.bottomMargin = this.getMeasuredHeight() - dp2px(buttonWidth) / 2 - y;
		btn_menu.setLayoutParams(layoutParams0);
		final int bottomMargins = this.getMeasuredHeight() - dp2px(buttonWidth) - layoutParams0.bottomMargin;
		for (int i = 0; i < btns.size(); i++) {
			final float xLenth, yLenth;
			if (isLeft) {
				xLenth = (float) (dp2px(r) * Math.sin((i - 1) * angle));
				yLenth = (float) (dp2px(r) * Math.cos((i - 1) * angle));
			} else {
				xLenth = (float) (dp2px(r) * Math.sin((i - 3) * angle));
				yLenth = (float) (dp2px(r) * Math.cos((i - 3) * angle));
			}
			LayoutParams layoutParams = layoutParams0;
			btns.get(i).setVisibility(View.VISIBLE);
			btns.get(i).setLayoutParams(layoutParams);
			btns.get(i).startAnimation(animTranslate(xLenth, -yLenth, layoutParams0.leftMargin + (int) xLenth,
					bottomMargins - (int) yLenth, btns.get(i), minTimeSpent + i * intervalTimeSpent, true));
		}

	}

	boolean isLeft;

	public void hideMenu() {
		btn_menu.setVisibility(View.GONE);
		LayoutParams layoutParams0 = (LayoutParams) btn_menu.getLayoutParams();
		final int bottomMargins = this.getMeasuredHeight() - dp2px(buttonWidth) - layoutParams0.bottomMargin;
		for (int i = 0; i < btns.size(); i++) {
			final float xLenth, yLenth;
			if (isLeft) {
				xLenth = (float) (dp2px(r) * Math.sin((i - 1) * angle));
				yLenth = (float) (dp2px(r) * Math.cos((i - 1) * angle));
			} else {
				xLenth = (float) (dp2px(r) * Math.sin((i - 3) * angle));
				yLenth = (float) (dp2px(r) * Math.cos((i - 3) * angle));
			}
			btns.get(i).setVisibility(View.GONE);
			btns.get(i).startAnimation(animTranslate(-xLenth, yLenth, layoutParams0.leftMargin, bottomMargins,
					btns.get(i), maxTimeSpent - i * intervalTimeSpent, false));
		}
	}

	final float scale() {
		return getContext().getResources().getDisplayMetrics().density;
	}

	public int dp2px(float dp) {
		return (int) (dp * scale() + 0.5f);
	}

	private AnimationSet animTranslate(float toX, float toY, final int lastX, final int lastY, final ImageView button,
			long durationMillis, boolean show) {

		AnimationSet animationSet = new AnimationSet(true);
		/*final AlphaAnimation alphaAnimation;
		if (show) {
			alphaAnimation = new AlphaAnimation(0, 1);
		} else {
			alphaAnimation = new AlphaAnimation(1, 0);
		}*/
		// alphaAnimation.setDuration(durationMillis);
		// animationSet.addAnimation(alphaAnimation);
		Animation animation = new TranslateAnimation(0, toX, 0, toY);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				params = new LayoutParams(0, 0);
				params.height = dp2px(buttonWidth);
				params.width = dp2px(buttonWidth);
				params.setMargins(lastX, lastY, 0, 0);
				button.setLayoutParams(params);
				button.clearAnimation();

			}
		});
		animation.setDuration(durationMillis);
		// if (lastX < this.getMeasuredWidth() - dp2px(buttonWidth)
		// && lastY < this.getMeasuredHeight() - dp2px(buttonWidth)) {
		animationSet.addAnimation(animation);
		// }
		return animationSet;
	}

	CALLBACK<Integer> _callback;

	ImageView testButton(int x, int y) {
		for (int i = 0; i < btns.size(); i++) {
			ImageView btn = (ImageView) btns.get(i);
			int left = btn.getLeft();
			int top = btn.getTop();
			if (x >= left && x <= left + dp2px(buttonWidth) && y >= top && y <= top + dp2px(buttonWidth)) {
				return btn;
			}
		}
		return null;
	}
/*
	private Animation animScale(float toX, float toY) {
		Animation animation = new ScaleAnimation(1.0f, toX, 1.0f, toY, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		animation.setInterpolator(context, anim.accelerate_decelerate_interpolator);
		animation.setDuration(400);
		animation.setFillAfter(false);
		return animation;

	}*/

	public void init(List<Drawable> icons, CALLBACK<Integer> callback) {
		for (int i = 1; i < this.getChildCount() - 1; i++) {
			this.removeViewAt(i);
		}
		this._callback = callback;
		//
		for (int i = 0; i < icons.size(); i++) {
			Drawable icon = icons.get(i);
			FlowerMenuItem menuButton = new FlowerMenuItem(getContext());
			menuButton.setLayoutParams(btn_menu.getLayoutParams());
			menuButton.setImageDrawable(icon);
			menuButton.setTag(i);
			menuButton.setVisibility(View.GONE);
			menuButton.setBackgroundColor(Color.TRANSPARENT);
			menuButton.setClickable(true);
			menuButton.setLongClickable(true);
			/*menuButton.setOnTouchListener(new OnTouchListener() {

				@SuppressLint("ClickableViewAccessibility")

				@Override
				public boolean onTouch(View arg0, MotionEvent arg1) {
					Log.e("--------", "xxx");
					switch (arg1.getAction()) {
					case MotionEvent.ACTION_OUTSIDE:
						break;
					case MotionEvent.ACTION_UP:
						if (_callback != null) {
							_callback.run(false, (Integer) arg0.getTag());
						}
						break;
					case MotionEvent.ACTION_DOWN:
						break;
					default:
						break;
					}
					return false;
				}

			});*/

			this.addView(menuButton, this.getChildCount() - 1);
			btns.add(menuButton);
		}

		intervalTimeSpent = 0;// (maxTimeSpent - minTimeSpent) / btns.size();//
								// 20
		angle = (float) Math.PI / 5;// (float)Math.PI/(2*(btns.size()-1));
	}

	
}
